package com.member.web.controller.shop;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.member.common.annotation.Log;
import com.member.common.core.controller.BaseController;
import com.member.common.core.domain.AjaxResult;
import com.member.common.core.domain.model.LoginUser;
import com.member.common.core.page.TableDataInfo;
import com.member.common.enums.BusinessType;
import com.member.common.utils.StringUtils;
import com.member.common.utils.poi.ExcelUtil;
import com.member.shop.domain.SysBizOtherUser;
import com.member.shop.service.ISysBizOtherUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 【用户层级】控制器
 *
 * @author wangxin
 * @version 1.0
 * @date 2024-06-11
 **/
@RestController
@RequestMapping("/shop/otherUser")
public class SysBizOtherUserController extends BaseController {
    @Resource
    private ISysBizOtherUserService sysBizOtherUserService;

    /**
     * 查询用户层级列表
     */
    @PreAuthorize("@ss.hasPermi('shop:otherUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBizOtherUser sysBizOtherUser) {
        startPage();
        LambdaQueryWrapper<SysBizOtherUser> queryWrapper = new LambdaQueryWrapper<SysBizOtherUser>();
        queryWrapper.and(StringUtils.isNotBlank(sysBizOtherUser.getSearchValue()), i -> {
            i.like(SysBizOtherUser::getUserName, sysBizOtherUser.getSearchValue()).or();
            i.like(SysBizOtherUser::getUserPhone, sysBizOtherUser.getSearchValue()).or();
        });
        List<SysBizOtherUser> list = sysBizOtherUserService.selectSysBizOtherUserList(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 导出用户层级列表
     */
    @PreAuthorize("@ss.hasPermi('shop:otherUser:export')")
    @Log(title = "用户层级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBizOtherUser sysBizOtherUser) {
        LambdaQueryWrapper<SysBizOtherUser> queryWrapper = new LambdaQueryWrapper<SysBizOtherUser>();
        List<SysBizOtherUser> list = sysBizOtherUserService.selectSysBizOtherUserList(queryWrapper);
        ExcelUtil<SysBizOtherUser> util = new ExcelUtil<SysBizOtherUser>(SysBizOtherUser.class);
        util.exportExcel(response, list, "用户层级数据");
    }

    /**
     * 获取用户层级详细信息
     */
    @PreAuthorize("@ss.hasPermi('shop:otherUser:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysBizOtherUserService.selectSysBizOtherUserById(id));
    }

    /**
     * 新增用户层级
     */
    @PreAuthorize("@ss.hasPermi('shop:otherUser:add')")
    @Log(title = "用户层级", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBizOtherUser sysBizOtherUser) {
        return toAjax(sysBizOtherUserService.insertSysBizOtherUser(sysBizOtherUser));
    }

    /**
     * 修改用户层级
     */
    @PreAuthorize("@ss.hasPermi('shop:otherUser:edit')")
    @Log(title = "用户层级", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBizOtherUser sysBizOtherUser) {
        return toAjax(sysBizOtherUserService.updateSysBizOtherUser(sysBizOtherUser));
    }

    /**
     * 删除用户层级
     */
    @PreAuthorize("@ss.hasPermi('shop:otherUser:remove')")
    @Log(title = "用户层级", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysBizOtherUserService.deleteSysBizOtherUserByIds(ids));
    }

    /**
     * 批量注册会员
     */
    @Log(title = "批量注册会员", businessType = BusinessType.IMPORT)
    @PostMapping("/registerImportData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<SysBizOtherUser> util = new ExcelUtil<SysBizOtherUser>(SysBizOtherUser.class);
        List<SysBizOtherUser> userList = util.importExcel(file.getInputStream());
        LoginUser loginUser = getLoginUser();
        String operName = loginUser.getUsername();
        String message = sysBizOtherUserService.importUser(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 批量注册会员 模板
     */
    @PostMapping("/registerTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<SysBizOtherUser> util = new ExcelUtil<>(SysBizOtherUser.class);
        util.importTemplateExcel(response, "用户数据");
    }
}
